import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.read_csv("stock_day.csv")
data = data.drop(["price_change", "p_change", "ma5", "ma10", "ma20", "v_ma5", "v_ma10", "v_ma20", "turnover"], axis=1)
print(data.head())
"""
             open   high  close    low    volume
2018-02-27  23.53  25.88  24.16  23.53  95578.03
2018-02-26  22.80  23.78  23.53  22.80  60985.11
2018-02-23  22.88  23.37  22.82  22.71  52914.01
2018-02-22  22.25  22.76  22.28  22.02  36105.01
2018-02-14  21.49  21.99  21.92  21.48  23331.04
"""
data["open"].add(5)
data[(data["open"] > 23) & (data["open"] < 24)].head()
data.query("open<24 & open>23").head()
data[data["open"].isin([23.23, 23.71])]
data.query("open in [23.23, 23.71]")

# 统计运算：min最小值；max最大值；mean平均值；median中位数；var方差；std标准差；mode众数
# 默认统计是按列，axis=0
print(data.describe())
"""
             open        high       close         low         volume
count  643.000000  643.000000  643.000000  643.000000     643.000000
mean    21.272706   21.900513   21.336267   20.771835   99905.519114
std      3.930973    4.077578    3.942806    3.791968   73879.119354
min     12.250000   12.670000   12.360000   12.200000    1158.120000
25%     19.000000   19.500000   19.045000   18.525000   48533.210000
50%     21.440000   21.970000   21.450000   20.980000   83175.930000
75%     23.400000   24.065000   23.415000   22.850000  127580.055000
max     34.990000   36.350000   35.210000   34.010000  501915.410000
"""

# 各列中统计最小值
print(data.min(0))
"""
open        12.25
high        12.67
close       12.36
low         12.20
volume    1158.12
dtype: float64
"""

# 各列中统计最小值对应的索引
print(data.idxmin(0))
"""
open      2015-03-02
high      2015-03-02
close     2015-09-02
low       2015-03-02
volume    2016-07-06
dtype: object
"""

# 自定义运算
data[["open", "close"]].apply(lambda x: x.max() - x.min(), axis=0)

plt.figure(figsize=(10, 8), dpi=80)
data["open"].cumsum().plot()
plt.show()
